﻿using System;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using Inkubator.Wizard.Model.Model;

namespace Inkubator.Wizard.Model.Xml
{
    public class XmlOperations
    {
        /// <summary>
        /// Method that create a XML File that represent the object
        /// received as argument.
        /// </summary>
        /// <param name="serializable"></param>
        /// <param name="fileName"></param>
        public void serialize( Object serializable, string fileName )
        {
            XmlSerializer serializer = new XmlSerializer( serializable.GetType() );

            StreamWriter writer = new StreamWriter( fileName );

            serializer.Serialize(writer, serializable);
            writer.Close();
        }

        /// <summary>
        /// This Method return an object representation from the XML File.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="pathXSD"></param>
        /// <returns></returns>
        public WizardModel deserialize(String path, String pathXSD)
        {
            XmlSerializer serializer = new XmlSerializer( typeof( WizardModel ) );
            XmlReader reader;
            if (pathXSD.Equals(""))
            {
                reader = XmlReader.Create(path);
            }
            else
            {
                reader = this.validate(path, pathXSD);
            }

            WizardModel wiz = (WizardModel) serializer.Deserialize(reader);
            reader.Close();

            return wiz;
        }

        /// <summary>
        /// This Method return an object representation from the File obtained
        /// through the Stream object..
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public WizardModel deserialize( Stream stream )
        {
            XmlSerializer serializer = new XmlSerializer( typeof( WizardModel ) );
            XmlReader reader = XmlReader.Create(stream);

            WizardModel wiz = (WizardModel) serializer.Deserialize(reader);
            reader.Close();

            return wiz;
        }

        private XmlReader validate( String path, String pathXSD )
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType = ValidationType.Schema;

            XmlSchemaSet schemas = new XmlSchemaSet();
            settings.Schemas = schemas;
            schemas.Add(null, pathXSD);
            settings.ValidationEventHandler += validationEventHandler;
            XmlReader reader = XmlReader.Create(path, settings);

            try
            {
                while (reader.Read()) {}
            }
            catch (XmlException err)
            {
                Console.WriteLine(err.Message);
                return null;
            }

            return reader;

        }

        private void validationEventHandler(object sender, ValidationEventArgs args)
        {
            Console.WriteLine("validation");//TODO
        }

    }
}